package com.shinhye.smartsms.provider;

import com.shinhye.smartsms.utils.SmsUris;

import android.app.SearchManager;
import android.content.SearchRecentSuggestionsProvider;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.BaseColumns;

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {

	public final static String AUTHORITY = "com.shinhye.MySuggestionProvider";
	public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;

	public MySuggestionProvider() {
		setupSuggestions(AUTHORITY, MODE);
	}
	
    String[] mProjection = new String[]{
    		"_id",
    		"address",
    		"body",
    		"body"
    };
	
    String[] columnNames = new String[]{
    		BaseColumns._ID,
    		SearchManager.SUGGEST_COLUMN_TEXT_1,
    		SearchManager.SUGGEST_COLUMN_TEXT_2,
    		SearchManager.SUGGEST_COLUMN_QUERY    //系统会拿着跟这个字段对应的数据，作为query的参数跳转到SearchableActivity
    };
    
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = getContext().getContentResolver().query(SmsUris.SMS_URI, mProjection, "body like ?", new String[]{"%" + selectionArgs[0] + "%"}, null);
		MatrixCursor mc = new MatrixCursor(columnNames);
    	//把cursor的内容复制到mc对象里
    	if(cursor.getCount() > 0){
	    	while(cursor.moveToNext()){
	    		String[] columnValues = new String[columnNames.length];
	    		columnValues[0] = cursor.getString(0);
	    		columnValues[1] = cursor.getString(1);
	    		columnValues[2] = cursor.getString(2);
	    		columnValues[3] = cursor.getString(3);
	    		mc.addRow(columnValues);
	    	}
    	}
		return mc;
	}
}
